package com.xmad.ksadutils.AdSDK.AdMold.Kwai;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import com.kwad.sdk.api.KsAdSDK;
import com.kwad.sdk.api.KsLoadManager;
import com.kwad.sdk.api.KsScene;
import com.kwad.sdk.api.KsSplashScreenAd;
import com.xmad.ksadutils.AdSDK.AdBase.AdBaseLoad;
import com.xmad.ksadutils.AdSDK.AdBase.AdCode;
import com.xmad.ksadutils.AdSDK.Interface.AdLoad;

public class KsLoadSplashScreenAd extends AdBaseLoad implements AdLoad {
    private volatile static KsLoadSplashScreenAd mInstance;
    KsSplashScreenAd ksSplashScreenAd;
    private int Position;
    /**
     * 单例模式
     *
     * @return
     */
    public static KsLoadSplashScreenAd getInstance() {
        if (mInstance == null) {
            synchronized (KsLoadSplashScreenAd.class) {
                if (mInstance == null) {
                    mInstance = new KsLoadSplashScreenAd();
                }
            }
        }
        return mInstance;
    }

    @Override
    public void Show(Activity activity, FrameLayout frameLayout) {
        Log.e(TAG,"开屏————————开始加载:");
        if (AdCode.getKsLoadSplashScreenId()==0&&KsAdId==null){
            Log.e(TAG,"KsLoadSplashScreenAd————————还未设置ID:");
            return;
        }
        Long id=KsAdId!=null ? KsAdId : AdCode.getKsLoadSplashScreenId();
        if (ToastCode){
            Log.e(TAG,KsAdId+"mmmmmmmmmmm");
            Log.e(TAG,id+"");
            Toast.makeText(activity,id+"",Toast.LENGTH_SHORT).show();
        }
        KsScene scene = new KsScene.Builder(id)
                //是否需要开屏小窗展示，默认为false, 设置false后将不会回调 onShowMiniWindow
                .needShowMiniWindow(true)
                .build(); // 此为测试posId，请联系快手平台申请正式posId
        KsAdSDK.getLoadManager().loadSplashScreenAd(scene, new KsLoadManager.SplashScreenAdListener() {
            @Override
            public void onError(int code, String msg) {
                Log.e(TAG,"onError----"+code+msg);
                if (KsAds!=null&&KsAds.size()>0){
                    if (Position>=KsAds.size()){
                        Position=0;
                            if (listener!=null){
                                listener.onJump(404);
                                return;
                            }
                        return;
                    }
                    AdCode.setKsLoadSplashScreenId(KsAds.get(Position));
                    Log.e("激励视频重试请求————ID:",AdCode.getKsLoadSplashScreenId()+"--"+Position);
                    ++Position;
                    KsAdId=null;
                    Show(activity,frameLayout);
                }else {
                    Log.e(TAG,"还未设置重试id:");
                }
            }
            @Override
            public void onRequestResult(int adNumber) {

            }
            @Override
            public void onSplashScreenAdLoad(@NonNull KsSplashScreenAd splashScreenAd) {
               // addView(splashScreenAd,frameLayout);
                ksSplashScreenAd = splashScreenAd;
                View view = splashScreenAd.getView(activity, new KsSplashScreenAd.SplashScreenAdInteractionListener() {
                    @Override
                    public void onAdClicked() {
                        ksSplashScreenAd = null;
                        if (listener!=null){
                            listener.onJump(200);
                        }
                    }

                    @Override
                    public void onAdShowError(int code, String extra) {
                        ksSplashScreenAd = null;
                    }

                    @Override
                    public void onAdShowEnd() {
                        if (listener!=null){
                            listener.onJump(200);
                            Log.e(TAG,"开屏————————跳转:");
                        }

                    }

                    @Override
                    public void onAdShowStart() {
                    }

                    @Override
                    public void onSkippedAd() {
                        if (listener!=null){
                            listener.onJump(400);
                        }
                    }

                    @Override
                    public void onDownloadTipsDialogShow() {

                    }

                    @Override
                    public void onDownloadTipsDialogDismiss() {

                    }

                    @Override
                    public void onDownloadTipsDialogCancel() {

                    }
                });

                if (!activity.isFinishing()) {
                    //ViewGroup root = activity.findViewById(R.id.frameLayout);
                    //root.removeAllViews();
                    view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                    frameLayout.addView(view);
                }
            }
        });
    }



}
